package helper.base

import helper.base.PrintHelper.StringTo.toPrint
import helper.base.PrintHelper.StringTo.toPrintln

object PrintHelper {
    object StringTo {
        fun String.toPrint() {
            print(this)
        }

        fun String.toPrintln() {
            println(this)
        }
    }

    object StringBuilderTo {
        fun StringBuilder.toPrint() {
            return this.toString().toPrint()
        }

        fun StringBuilder.toPrintln() {
            return this.toString().toPrintln()
        }
    }

    fun Any?.toPrettyString(indent: String = ""): String {
        val builder = StringBuilder()

        when (this) {
            is Map<*, *> -> {
                builder.append("{\n")
                this.entries.forEach { (key, value) ->
                    builder.append("$indent  $key : ${value.toPrettyString("$indent  ")},\n")
                }
                builder.append("$indent}")
            }

            is List<*> -> {
                builder.append("[\n")
                this.forEach { item ->
                    builder.append("$indent  ${item.toPrettyString("$indent  ")},\n")
                }
                builder.append("$indent]")
            }

            is Pair<*, *> -> {
                builder.append(
                    "(\n$indent  ${this.first.toPrettyString("$indent  ")},\n$indent  ${
                        this.second.toPrettyString(
                            "$indent  "
                        )
                    }\n$indent)"
                )
            }

            null -> builder.append("null")
            else -> builder.append(this.toString())
        }

        return builder.toString()
    }

    fun varargPrintln(vararg message: Any?) {
        message.forEach {
            print("$it,")
        }
        println(message)
    }
}